<?php 
   require_once 'config.php';
   $action = getValue('action','str','POST','');
   if($action == 'add'){
      $book_name = getValue('book_name','str','POST','');
      $book_content = getValue('book_content','str','POST','');
      $book_num_page = getValue('book_num_page','str','POST','');
      $book_price = getValue('book_price','str','POST','');
      $book_cat_id = getValue('book_cat_id','int','POST',0);
      $book_pub_id = getValue('book_pub_id','int','POST',0);
      $aut_id = getValue('aut_id','str','POST','');
      $book_image = '';
      $book_date = time();
      if(Form::isValid("myForm")){
         if(isset($_FILES['image_field'])){
            $handle = new upload($_FILES['image_field'],'vn_VN');
            if ($handle->uploaded) {
               $handle->allowed = array('image/*');
               $handle->file_max_size = 2*1024*1024;
               $handle->file_new_name_body   = dechex(rand(100,999)).Str::IDEncode(time());
               $handle->image_resize         = true;
               $handle->image_x              = 400;
               $handle->image_ratio_y        = true;
               $handle->process(PATH_IMG_UPLOAD);
               $book_image = $handle->file_dst_name;
               if ($handle->processed) {
                   $handle->clean();
               } else {
                  $error_upload = Str::alert($handle->error,'alert-error');
               }
           }
         }
         $sql = "INSERT INTO
                     books(
                        book_name,
                        book_image,
                        book_date_create,
                        book_date_modify,
                        book_cat_id,
                        book_pub_id,
                        book_num_page,
                        book_price,
                        book_content,
                        book_active,
                        book_feature
                        )
                     VALUES(
                        :book_name,
                        :book_image,
                        :book_date_create,
                        :book_date_modify,
                        :book_cat_id,
                        :book_pub_id,
                        :book_num_page,
                        :book_price,
                        :book_content,
                        :book_active,
                        :book_feature)";
         $value = array('book_name' => $book_name,
                         'book_image' => $book_image,
                         'book_date_create' => $book_date,
                         'book_date_modify' => $book_date,
                         'book_cat_id' => $book_cat_id,
                         'book_pub_id' => $book_pub_id,
                         'book_num_page' => $book_num_page,
                         'book_price' => $book_price,
                         'book_content' => $book_content,
                         'book_active' => 1,
                         'book_feature' => 0
                         );
         $db = new DB();
         $insert = $db->query($sql,$value);
         if($insert){
            if($aut_id != ''){
               $arr_aut_id = explode(',',$aut_id);
               $last_id = $db->lastInsertId();
               foreach($arr_aut_id as $item){
                  $sql = "INSERT INTO book_author(book_id,aut_id) VALUES(:book_id,:aut_id)";
                  $value = array("book_id" => $last_id,"aut_id" => $item);
                  $db->query($sql,$value);   
               }   
            }
           $alert = Str::alert("Thêm mới thành công"); 
         } 
      }
      else{
         Form::setError("myForm","Điền đầy đủ thông tin cho sản phẩm");
      }
   }
?>
<!DOCTYPE HTML>
<head>
	<meta http-equiv="content-type" content="text/html" />
	<meta name="author" content="Ta Van Chinh" />
   <?php echo $css_module ?>
   <?php echo $js_module ?>
	<title>Thêm mới sản phẩm</title>
</head>

   <body>
      <div class="head">
         <div class="isw-grid"></div>
         <h1>Thêm mới sản phẩm</h1>      
         <div class="reload_tab" title="Reload"></div>                       
         <div class="clear"></div>
      </div>
   <div class="block-fluid">
   <?php echo isset($alert) ? $alert : ""?>
   <?php 
      $form = new Form("myForm");
      $form->addElement(new Element_Hidden('action','add'));
      $form->configure(array(
      	"prevent" => array("bootstrap", "jQuery")
      ));
      $form->addElement(new Element_HTML('<div class="requie">Những dòng đánh dấu * là bắt buộc nhập</div>'));
      $form->addElement(new Element_Textbox('Tên sách:','book_name',array(
                        'id' => 'pro_name',
                        'placeholder' => 'Tên sách',
                        'required' => 1)
                        ));        
      $form->addElement(new Element_File('Ảnh 1(<2MB):','image_field',array(
                        'id' => 'image_field',
                        'required' => 1,
                        'accept' =>"image/*"
                        )));
      $form->addElement(new Element_Textbox('Số trang:','book_num_page',array(
                        'id' => 'book_num_page',
                        'placeholder' => 'Số trang',
                        'required' => 1)
                        ));  
      $form->addElement(new Element_Textbox('Giá bán:','book_price',array(
                        'id' => 'book_price',
                        'placeholder' => 'Giá bán',
                        'required' => 1)
                        ));
      $form->addElement(new Element_Select('Chuyên mục:','book_cat_id',$cat_option,array(
                        'id' => 'book_cat_id'
                        )));  
      $form->addElement(new Element_Select('Nhà xuất bản:','book_pub_id',$pub_option,array(
                        'id' => 'book_pub_id'
                        )));
      $form->addElement(new Element_Textbox('Tác giả:','aut_id',array('id' => 'aut_id')));                     
      $form->addElement(new Element_Textarea('Tóm tắt nội dung:','book_content',array(
                        'id' => 'book_content',
                        'placeholder' => 'Tóm tắt')
                        ));
      
      $form->addElement(new Element_Button("Thêm mới","submit", array(
         'name' => 'submit',
         'id' => 'submit'
      )));
      $form->addElement(new Element_Button("Hủy", "button", array(
          "onclick" => "history.go(-1);"
      )));
      $form->render();
   ?>
</div>
<script>
   $(document).ready(function(){
      $("#aut_id").tokenInput("ajax_token_input_person.php", {
         theme: "facebook",
         hintText:"Gõ tên tác giả",
         noResultsText:"Không tìm thấy",
         searchingText:"Đang tìm...",
         preventDuplicates: true,
      });
      CKEDITOR.replace( 'book_content',{
          enterMode:CKEDITOR.ENTER_BR,
      }); 
   });
   
</script>
</body>
</html>
